<?xml version="1.0" encoding="utf-8"?> 
<%@ page language="java" contentType="text/html; charset=utf-8" 
pageEncoding="utf-8"%> 
<%@ page import="java.util.*"%> 
<%@ page import = "com.tomato.dao.PMF" %>
<%@ page import = "javax.jdo.PersistenceManager" %>
<%@ page import = "javax.jdo.Query" %>
<%@ page import = "java.util.List" %>
<%@ page import = "javax.jdo.Transaction" %>
<%@ page import = "com.google.appengine.api.datastore.Key" %>
<%@ page import = "com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import = "ebook.server.Member" %>
<%@ page import = "com.tomato.bean.Ebook" %>
<%@ page import="org.apache.juli.logging.Log" %>
<%@ page import="org.apache.juli.logging.LogFactory"%>
<%@ page import = "com.google.appengine.api.datastore.Blob" %>

<% 
final Log log = LogFactory.getLog("");
log.info("여기왔나-1");
Member e;
Ebook p;
log.info("여기왔나-2");
int result = 0;
String sid = null, res = "res";
String author = null, stock = null, description = null, title = null;
Long id = null;
Blob image = null;
int count = 0;
String email = request.getParameter("email");
String bookmaker = request.getParameter("bookid"); 
log.info("아이디:"+email);
log.info("책키값:"+bookmaker);
PersistenceManager pm = PMF.get().getPersistenceManager();
log.info("여기왔나1");
Query query = pm.newQuery(Member.class);
log.info("여기왔나2");
@SuppressWarnings("unchecked")
List<Member> emp = (List<Member>)query.execute();
log.info("여기왔나3");
for(Member emps : emp){
	if(emps.getEmail().equals(email)){
		sid = emps.getId().toString();
		log.info("여기왔나4"+sid);
	}
}
String[] bunri = null, bunri2 = null;
String book = null, book2 = null;
log.info("여기왔나5");
Key key1 = KeyFactory.createKey(Member.class.getSimpleName(), Integer.parseInt(sid));
log.info("여기왔나6");
e = pm.getObjectById(Member.class, key1);
log.info("여기왔나7"+sid);
book2 = e.getBookmaker();
log.info("여기왔나7.1"+book2+bunri2);
bunri2 = book2.split("@");
log.info("여기왔나7.1"+book2+bunri2[0]);
for(int l = 0; l <bunri2.length; l++){
	if(bunri2[l].equals(bookmaker)){
		res = bookmaker;
		log.info("여기왔나8"+res);
	}
}log.info("여기왔나8"+res);
if(!(res.equals(bookmaker))){
	e.setBookmaker(e.getBookmaker()+"@"+ bookmaker);
}
log.info("북메이커키값들:"+e.getBookmaker());
Key key2 = KeyFactory.createKey(Ebook.class.getSimpleName(), Integer.parseInt(bookmaker));
p = pm.getObjectById(Ebook.class, key2);
count = p.getCount();
if(!(res.equals(bookmaker))){
	p.setCount(count+1);
	log.info("여기왔나8"+count);
	log.info("여기왔나8"+p.getCount());
}
log.info("여기왔나9"+p.getCount());
book = e.getBookmaker();
bunri=book.split("@");
int i = bunri.length;
byte[] a = p.getEbook().getBytes();
String ebook = new String(a,"UTF-8");
a = null;
StringBuffer sb = new StringBuffer();  
   	for(int k = 0; k < ebook.length(); k++){  
       if(ebook.charAt(k) == '&'){ 
          sb.append("amp;");  
       }else if(ebook.charAt(k) == '<'){  
          sb.append("lt;");
       }else if(ebook.charAt(k) == '>'){  
          sb.append("gt;");  
       }else  
          sb.append(ebook.charAt(k));  
    }
ebook = sb.toString();
log.info("여기왔나10");
%>
<prov>
<%
	
	for(int k = 0; k<bunri.length; k++){
		log.info("여기왔나11");
		Key key3 = KeyFactory.createKey(Ebook.class.getSimpleName(), Integer.parseInt(bunri[k]));
		p = pm.getObjectById(Ebook.class, key3);
		log.info("여기왔나12");
		author = p.getAuthor();
		log.info("여기왔나13");
		stock = p.getStock();
		description = p.getDescription();
		title = p.getTitle();
		id = p.getId();
		count = p.getCount();
		image = p.getImage();
		if(((bunri.length)-1) == k){
			result = 1;
			log.info("여기왔나4"+bunri[k]);
			pm.close();
		}
	
		log.info("여기왔나5"+bunri[k]);
%> 
<lists> 
<data>
<rowid>1</rowid>
<id><%= id %></id>
<title><%= title %></title>
<author><%= author %></author>
<description><%= description %></description>
<% if(image != null){ %>
<imageurl><%=request.getServerName()%>/e_book?method=show@amp;id=<%=p.getId()%></imageurl>
<% }else {%>
<imageurl>No image</imageurl>
<% } %>
<stock><%= stock %></stock>
<% if(result == 1) { %>
<bookmaker><%= bookmaker %></bookmaker>
<ebook><%= ebook %></ebook>
<getsu><%= i+1 %></getsu>
<% } %>
</data>
</lists>
<% } %>
</prov>